/**
 *  @file Collections.java
 *  @author Mamontov
 *
 *  This file contains Collections class definition
 */
package db21.entities;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.rdf.model.Property;
import db21.racerpro.Queries;
import java.util.ArrayList;

/**
 * @class  Collections
 * @author Mamontov
 *
 *
 */
public class Collections extends Entities {
    
    public Collections() {
        m_conceptname = "Collection";
    }
    
    public Collection fetch(String iri) {
         Collection result = new Collection();
         result.name = iri;
         
         String escaped = Queries.escapeValue(m_conn.prefix(), iri);
         String q;
         ArrayList<String> list;
         
         q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.includeResources());
         list = this.fetchAsRow(q);
         result.resources = list;
        
         return result;
     }
    
    /**
     * You should use only this function to create new resource, because only
     * it cares about necessary assertion
     * @param resource
     * @return whether it was successfully created
     */
    public boolean add(Collection collection) {
        Individual ind = this.makeAndReturnIndividual(m_conceptname, collection.name);
        if (ind == null) return false;
        Property prop;
        Individual right;
        
        // Assert competences
        prop = m_conn.getProperty(Queries.includeResources());
        for(int i = 0; i < collection.resources.size(); i++) {
            right = m_conn.getIndividual(collection.resources.get(i));
            ind.addProperty(prop, right);
        }
        m_conn.syncWithRacerPro();
        return true;
    }
}
